<!-- 
    @Name：查看得分
    @Author：tianke
    @Site：http://www.layui.com/admin/
    @License：LPPL 
-->
<script type="text/html" template lay-done="layui.data.viewScoreDone(d)">
    <div class="layui-fluid custom-viewScore-page">
        <table id="my-viewScore-table" lay-filter="viewScore-table-filter"></table>
        <div class="layui-row layui-col-space12">
            <div class="layui-col-md8">
                <div class="layui-card layui-card-left">
                    <div class="layui-card-header">计算过程:</div>
                    <div class="layui-card-body">
                        <div class="layui-card-content">
                            <div class="layui-form-item">
                                <label class="layui-form-label">公式：</label>
                                <div class="layui-input-block">
                                    <span class="layui-formula formula"></span>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">参数：</label>
                                <div class="layui-input-block">
                                    <span class="layui-formula params"></span>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">结果：</label>
                                <div class="layui-input-block">
                                    <span class="layui-formula datascore"></span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-col-md4">
                <div class="layui-card layui-card-right">
                    <div class="layui-card-header">函数说明:</div>
                    <div class="layui-card-body">
                        <div class="layui-card-content" id="formula-content"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</script>

<style>
    .layui-card-left, .layui-card-right{
        overflow: hidden;
    }
    .layui-table-view .layui-table td{
        vertical-align: middle;
    }
    .custom-viewScore-page .layui-form-label{
        padding: 0;
    }
    .custom-viewScore-page .layui-input-block{
        min-height: 20px;
        line-height: 20px;
    }
    .layui-card-left span{
        display: inline-block;
        line-height: 20px;
    }
    .custom-viewScore-page .layui-card-header{
        border: none
    }
    .custom-viewScore-page .layui-card-body{
        border: 1px solid #D0D3DB;
    }
    .layui-card-left .layui-card-body{
        height: 140px;
    }
    .layui-card-right .layui-form-item{
        margin-bottom: 0px;
    }
    .custom-viewScore-page .layui-table-box{
        border-bottom: 1px solid #e6e6e6;
    }
</style>

<!-- 函数说明模板 -->
<script type="text/html" id="formula">
    {{# if(d && d.length > 0){ }}
        {{# layui.each(d, function(index, item){ }}
            <div class="layui-form-item">
                <label class="layui-form-label">{{item.text}}：</label>
                <div class="layui-input-block">
                    <span class="layui-formula">{{item.func}}</span>
                </div>
            </div>
        {{# }) }}   
    {{# } }}
</script>

<script>
    layui.data.viewScoreDone = function(d){
        layui.use(['table', 'admin', 'tableMerge', 'laytpl'], function(){
            var table = layui.table,
                admin = layui.admin,
                tableMerge = layui.tableMerge,
                laytpl = layui.laytpl;

            var taskid = d.params.taskid ? d.params.taskid : '',
                deptid = d.params.deptid ? d.params.deptid : '',
                isAll = d.params.isAll ? d.params.isAll : '',
                allFormulas = [];

            switch(isAll){
                case 'true':
                    if(deptid){
                        initAllTable(taskid, deptid);
                    }else{
                        initAllTable(taskid);
                    }
                break;
                case 'false':
                    if(deptid){
                        initTable(taskid, deptid);
                    }else{
                        initTable(taskid);
                    }
                break;
                default:
                break;
            }
            initFormula();

            /*
                数据查询（查看得分）
            */
            function initTable(taskid, deptid){
                admin.req({
                    url: 'myapi/DC/api/rest/web/custom/DCContestPoint',
                    data: {
                        taskid: taskid,
                        dept_id: deptid
                    },
                    done: function(res){
                        if(res.data.retcode == 0){
                            var head = res.data.data.head,
                                data = res.data.data.data,
                                toolCols = [
                                    {field: 'datascore', align: 'center', width: 200}
                                ];
                            for(var i = 0; i < head.length; i++){
                                if(i == 0){
                                    head[i].merge = true;
                                }else{
                                    head[i].merge = [head[i-1].field, head[i].field];
                                }
                                head[i].minWidth = 200;
                                head[i].align = 'center';
                                (function(i){
                                    head[i].templet = function(d){
                                        var field = d[head[i].field],
                                            id = head[i].field+'_id',
                                            fieldId = d[id] || '',
                                            reg = new RegExp(/\((.*)\)/);
                                        if(field != 'datascore'){
                                            if(field.indexOf('(') != -1){
                                                var val = field.match(reg),
                                                    newfield = field.replace(reg, '<span style="color:red;">'+val[0]+'</span>');
                                                return '<span class="subItem" data-id='+fieldId+' lay-tips='+field+'>'+newfield+'</span>'
                                            }else{
                                                return '<span class="subItem" data-id='+fieldId+' lay-tips='+field+'>'+field+'</span>'
                                            }
                                        }
                                    };
                                })(i);
                            }
                            head = head.concat(toolCols);
                            table.render({
                                elem: '#my-viewScore-table',
                                height: 350,
                                text: {
                                    none: '<div class="no-data-tips"><img src="/dist/style/res/about/empty.png"><p style="color:#DBDBDB;">暂无数据</p></div>'
                                },
                                limit: 10000,
                                data: data,
                                cols: [head],
                                done: function(res, curr, count){
                                    if(res.data && res.data.length > 0){
                                        tableMerge.render(this);
                                        initCalculate(res.data[0]['1_Index_id'])
                                    }
                                }
                            })
                        }
                    }
                })
            }

            //审核管理（查看得分）
            function initAllTable(taskid, deptid){
                admin.req({
                    url: 'myapi/DC/api/rest/web/custom/DCAuditGetScoreTable',
                    data: {
                        taskid: taskid,
                        dept_id: deptid
                    },
                    done: function(res){
                        if(res.data.retcode == 0){
                            var head = res.data.data.datascore.head,
                                data = res.data.data.datascore.data,
                                toolCols = [
                                    {field: 'dataval', align: 'center', width: 200, templet: function(d){
                                        if(!!d.write_value){ //writeval不为空
                                            return "<span lay-tips="+d.write_value+">"+d.write_value+"</span>"
                                        }else if(!!d.show_value){
                                            return "<span lay-tips="+d.show_value+">"+d.show_value+"</span>"
                                        }else{
                                            return "<span lay-tips="+d.datascore+">"+d.datascore+"</span>"
                                        }
                                    }
                                    }
                                ];
                            allFormulas = res.data.data.middles;
                            for(var i = 0; i < head.length; i++){
                                if(i == 0){
                                    head[i].merge = true;
                                }else{
                                    head[i].merge = [head[i-1].field, head[i].field];
                                }
                                head[i].minWidth = 200;
                                head[i].align = 'center';
                                (function(i){ 
                                    head[i].templet = function(d){
                                        var field = d[head[i].field],
                                            id = head[i].field+'_id',
                                            fieldId = d[id] || '',
                                            reg = new RegExp(/\((.*)\)/);
                                        if(field != 'datascore'){
                                            if(field.indexOf('(') != -1){
                                                var val = field.match(reg),
                                                    newfield = field.replace(reg, '<span style="color:red;">'+val[0]+'</span>');
                                                return '<span class="eachItem" data-id='+fieldId+' lay-tips='+field+'>'+newfield+'</span>'
                                            }else{
                                                return '<span class="eachItem" data-id='+fieldId+' lay-tips='+field+'>'+field+'</span>'
                                            }
                                        }
                                    };
                                })(i);
                            }
                            head = head.concat(toolCols);
                            table.render({
                                elem: '#my-viewScore-table',
                                height: 350,
                                text: {
                                    none: '<div class="no-data-tips"><img src="/dist/style/res/about/empty.png"><p style="color:#DBDBDB;">暂无数据</p></div>'
                                },
                                limit: 10000,
                                data: data,
                                cols: [head],
                                done: function(res, curr, count){
                                    if(res.data && res.data.length > 0){
                                        tableMerge.render(this);
                                        initQueryCal(allFormulas[0]);
                                    }
                                }
                            })
                        }
                    }
                })
            }

            //加载函数说明
            function initFormula(){
                admin.req({
                    url: 'myapi/DC/api/rest/web/custom/DCFunctionShow',
                    data: {},
                    done: function(res){
                        var result = res.data;
                        if(result.retcode == 0){
                            var getTpl = formula.innerHTML,
                                view = document.getElementById('formula-content');
                            laytpl(getTpl).render(result.data, function(html){
                                view.innerHTML = html;
                            })
                        }
                    }
                })
            }

            //数据查询(所有)加载计算过程
            function initQueryCal(eachFormula){
                $('.formula').html(eachFormula.formula);
                $('.params').html(eachFormula.params);
                $('.datascore').html(eachFormula.result);
            }


            //加载计算过程
            function initCalculate(id){
                admin.req({
                    url: 'myapi/DC/api/rest/web/custom/DCGetIndexFormAndParAndScore',
                    data: {
                        id: id
                    },
                    done: function(res){
                        var result = res.data,
                            formula = '',
                            params = '',
                            datascore = '';
                        if(result.retcode == 0){
                            formula = result.data.formula;
                            params = result.data.params;
                            datascore = result.data.datascore;
                        }
                        $('.formula').html(formula);
                        $('.params').html(params);
                        $('.datascore').html(datascore);
                    }
                })
            }

            //指标数据各项点击事件
            $('.custom-viewScore-page').delegate('.subItem', 'click', function(){
                var indexId = $(this).data('id');
                initCalculate(indexId);
            })

            //数据查询(所有)各项点击事件
            $('.custom-viewScore-page').delegate('.eachItem', 'click', function(){
                var indexId = $(this).data('id');
                layui.each(allFormulas, function(index, item){
                    if(item.datascore_id == indexId){
                        initQueryCal(item);
                    }
                })
            })
        })
    }
</script>